/* FluxamasynthDrumPatternPlayer.pde
 * A drum machine for the Modern Device Fluxamasynth Shield
 * ModernDevice.com
 *
 * By Michel Gutlich 26-2-2011 
 * A sensor signal on analog input 0 gives some tempo dynamics
 * Contact at iching@xs4al.nl
 *
 * Updated May 2012, Love Nystrom
 * Sensor commented out in favor of fixed tempo.
 * Added new FlexamySynth features.
 */

#include <FlexamySynth.h>
#include <PgmChange.h>

FlexamySynth synth; // create a synth object

// Our drum pattern
// Every array cell is the velocity of the note played 
// Tick         1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
byte bd  [] = {127,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 90,  0}; //Bassdrum
byte sn  [] = {  0,  0,  0,  0,127,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0}; //Snare
byte hho [] = {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,127,  0}; //Hihat Open
byte hhc [] = {127, 40, 80, 40,127, 40, 80, 40,127, 40, 80, 40,127,  0,  0}; //Hihat Close
byte hhp [] = {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,127}; //Hihat Pedal

byte patternSize = 16;  // A pattern of max 16 ticks 
     //(actualy 15, because we loop and 16 is tick no.1.. you dig it ?)
byte tickNo;            // Our tick number variable

// Some basic settings

byte tempo   = 120;       // Initial tempo
byte myKick  = DR_Kick;
byte mySnare = DR_Snare;

// setup

void setup() 
{
  synth.begin();
  synth.midiReset(); // Do a complete MIDI reset
  synth.GS_Reset();  // Enable GS mode
  
  synth.programChange( DRUM_CHAN, DRUMS_Std );
  synth.setChannelVolume( DRUM_CHAN, 127 ); // max channel volume
  //synth.setReverbSend( DRUM_CHAN, 100 );
  //setDrumMix( Channel,DrumNr,Level,Pan,Reverb,Chorus )
  synth.setDrumMix( DRUM_CHAN, myKick,         120, CTV_CENTER, 80,  0 );
  synth.setDrumMix( DRUM_CHAN, mySnare,        110, CTV_CENTER-10, 72,  127 );
  synth.setDrumMix( DRUM_CHAN, DR_ClosedHiHat, 120, CTV_CENTER+10, 110, 0 );
  synth.setDrumMix( DRUM_CHAN, DR_OpenHiHat,   115, CTV_CENTER-10, 110, 0 );
  synth.setDrumMix( DRUM_CHAN, DR_PedalHiHat,  125, CTV_CENTER+10, 110, 0 );

  //setReverb( Program, Time, Feedback, Character )
  synth.setReverb( REV_DELAY, 127, 100, 4 );
  synth.setReverbLevel( 120 );  // Master reverb return level
  synth.setMasterVolume( 127 );	// Max master volume
}

// subs

void hitDrum( byte drumNr, byte velocity )
{
  synth.noteOn( DRUM_CHAN, drumNr, velocity ); // play a note
  synth.noteOff( DRUM_CHAN, drumNr );         // send note off
}

void tick()
{
  delay( tempo );
}

// loop

void loop() 
{
  for( byte tickNo=0; tickNo < patternSize; tickNo++ )
  {
    //tempo = analogRead( A0 ); // For live tempo dynamics :-)
    if (bd[tickNo]) hitDrum( myKick, bd[tickNo] );
    if (sn[tickNo]) hitDrum( mySnare, sn[tickNo] );
    if (hhc[tickNo]) hitDrum( DR_ClosedHiHat, hhc[tickNo] );
    if (hho[tickNo]) hitDrum( DR_OpenHiHat, hho[tickNo] );
    if (hhp[tickNo]) hitDrum( DR_PedalHiHat, hhp[tickNo] );
    tick();    
  }
} 

